package chaw.android.train.MoreActivities;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import chaw.android.train.MoreActivity;
import chaw.android.train.R;
import chaw.android.train.TrainTravelActivity;
import chaw.android.util.HttpRequestManager;
import chaw.android.util.Preferences;
import chaw.android.util.ServerURL;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MoreDevActivity extends Activity {
	private ImageView btnOk, btnBack;
	private EditText editDev;
	private TextView devLength;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_dev);
		
		btnOk = (ImageView) findViewById(R.id.btnOk);
		btnBack = (ImageView) findViewById(R.id.btnBack);
		editDev = (EditText) findViewById(R.id.editDev);
		devLength = (TextView) findViewById(R.id.devLength);
		
		btnOk.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(editDev.getText().length()>9)
					sendDevToServer();
				else
					Toast.makeText(getBaseContext(), "10글자 이상 입력해주세요.", Toast.LENGTH_SHORT).show();
			}
		});
		
		btnBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				MoreActivity parentActivity = (MoreActivity)getParent();
				parentActivity.finishFromChild(MoreDevActivity.this);
			}
		});
		
		editDev.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				devLength.setText(""+s.length());
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			
			@Override
			public void afterTextChanged(Editable s) {}
		});
	}
	
	private void sendDevToServer(){
		UploadAsynkTask task = new UploadAsynkTask();
		String[] params = new String[] { ServerURL.submitOpinion, "user_id", Preferences.getValue(getBaseContext(), "user_id"), "uuid_check", 
				Preferences.getValue(getBaseContext(), "uuid"), "text", editDev.getText().toString(), "type", "developer" };
		task.execute(params);
	}
	
	private class UploadAsynkTask extends AsyncTask<String, Void, String> {
		@Override
		protected String doInBackground(String... urls) {
			// TODO Auto-generated method stub
			String response = null;
			
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			
			if(urls.length > 1)
				for(int i = 1; i< urls.length; i+=2)
					params.add(new BasicNameValuePair(urls[i], urls[i+1]));
			response = HttpRequestManager.request(urls[0], params);
			return response;
		}
		
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			
			if("success".equalsIgnoreCase(result)){
				Toast.makeText(getBaseContext(), "의견을 보내주셔서 감사합니다.", Toast.LENGTH_SHORT).show();
				editDev.setText("");
			}
			else
				Toast.makeText(getBaseContext(), "오류 : " + result, Toast.LENGTH_SHORT).show();
		}
	}
	
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		for(int i = 0 ; i < 5; i++)
        	TrainTravelActivity.tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bg_nav1);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		for(int i = 0 ; i < 5; i++)
        	TrainTravelActivity.tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bg_nav1);
	}
}
